package citasManager;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Searcher {
	 Hashtable det;
	 String[] matchesArray;
	 
	 public Searcher(){
		 det = new Hashtable();		
	 }
	 
	 public String[] search(String regex, String onText)
	    {
	        Pattern regPatt = Pattern.compile(regex);
	        Matcher regMatch = regPatt.matcher(onText);
	        ArrayList<String> matches = new ArrayList<String>();
	        

	        while(regMatch.find())
	        {
	            String match = regMatch.group();
	            matches.add(match);
	        }
	       
	        matchesArray = new String[matches.size()];
	        matches.toArray(matchesArray);
	        return matchesArray;
	    }
	   
	    public void printMatches(String[] matches)
	    {
	        int matchesCount = matches.length;
	       
	        for(int i = 0; i < matchesCount; i++)
	        {
	            System.out.println(matches[i]);
	        }
	    }
	    
	    public boolean hayMatch(){
	    	return (matchesArray.length>0);
	    }
	    
	    public Hashtable getDetail(){
	    	StringTokenizer st ;
	    	int matchesCount = matchesArray.length;
	    	Vector aux = new Vector();
	    	for(int i = 0; i < matchesCount; i++)
	        {
	    		st = new StringTokenizer(matchesArray[i], ",");
	    		while(st.hasMoreTokens()) {
	    			aux.add(st.nextToken());		            
	    		}
	    		det.put(i,aux.clone());
	    		aux.clear();
	        }
	    	return det;
	    }

}
